﻿using System;
using System.Collections.Generic;
using System.Text;

using MQModelHandleLibaryCore.Model;
using MQModelHandleLibaryCore.RPCHandle;

namespace LdtDataEngin.ModuleDetail.BaseModule
{
    public class MQDataSource
    {
        /// <summary>
        /// 端口号集合
        /// </summary>
        private string[] _Attrib;
        /// <summary>
        /// 连接服务名IP
        /// </summary>
        private string[] hostname;
        /// <summary>
        /// MQ连接对象
        /// </summary>
        private ConnectionConfig connectionConfig;
        /// <summary>
        /// 运行参数
        /// </summary>
        private IDictionary<string, object> arguments = new Dictionary<string, object>();

        /// <summary>
        /// 实例化对象
        /// </summary>
        /// <param name="connstr">连接字符串</param>
        public MQDataSource(string connstr)
        {


            _Attrib = connstr.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < _Attrib.Length; i++)
            {
                _Attrib[i] = _Attrib[i].Substring(_Attrib[i].IndexOf("=") + 1);
            }
            hostname = _Attrib[0].Split(':');
            //进行实例化MQ通讯对象
            connectionConfig = new MQModelHandleLibaryCore.Model.ConnectionConfig()
            {
                Hostname = hostname[0],
                Port = Convert.ToInt32(hostname[1]),
                Pwssword = _Attrib[3],
                Username = _Attrib[2],
                Virtualhost = _Attrib[1],
                AutomaticRecoveryEnabled = true
            };

            arguments["x-max-priority"] = 10;//定义队列优先级为10个级别
            arguments["x-dead-letter-exchange"] = "DeadLetterExchange";
            arguments["x-dead-letter-routing-key"] = "DeadLetter";
            arguments.Add("x-message-ttl", 3600000);//队列上消息过期时间，应小于队列过期
        }


        /// <summary>
        /// 获取人员基础信息
        /// </summary>
        /// <param name="SourceName">案件类型-----暂时不启用</param>
        /// <param name="queuecode">队列编号</param>
        /// <param name="Content">原文</param>
        /// <returns></returns>
        public string ExtractPersonneNameList(string SourceName, string Content, string queuecode)
        {
            Random rnd = new Random(Guid.NewGuid().GetHashCode());
            string rndStr = rnd.Next().ToString();
            RPCRSend rPCRSend = new RPCRSend(connectionConfig, new MQModelHandleLibaryCore.Model.QueueModel()
            {
                QueueName = queuecode,
                Qdurable = true,
                QautoDelete = false,
                Qexclusive = false,
                ArgumentList = arguments
            });
            string message = rPCRSend.CallAsync(queuecode + "@@@" + Content.Trim() + "@@@" + rndStr, "Paragraphanalysis").Result;
            return message;
        }

        /// <summary>
        /// AI通过提取方法
        /// </summary>
        /// <param name="Content">原文</param>
        /// <param name="queuecode">队列编号</param>
        /// <returns></returns>
        public string AIExtract(string Content, string queuecode)
        {
            Random rnd = new Random(Guid.NewGuid().GetHashCode());
            string rndStr = rnd.Next().ToString();
            RPCRSend rPCRSend = new RPCRSend(connectionConfig, new MQModelHandleLibaryCore.Model.QueueModel()
            {
                QueueName = queuecode,
                Qdurable = true,
                QautoDelete = false,
                Qexclusive = false,
                ArgumentList = arguments
            });
            // 参数说明:队列编号+"@@@+原文+"@@@"+随机ID，队列名称
            string message = rPCRSend.CallAsync(queuecode + "@@@" + Content + "@@@" + rndStr, "Paragraphanalysis").Result;
            return message;
        }

        /// <summary>
        /// 获取人员发言人代称信息
        /// </summary>
        /// <param name="SourceName">案件类型</param>
        /// <param name="Content">原文</param>
        /// <returns></returns>
        public string ExtractSpokesNameList(string SourceName, string Content)
        {
            Random rnd = new Random(Guid.NewGuid().GetHashCode());
            string rndStr = rnd.Next().ToString();
            RPCRSend rPCRSend = new RPCRSend(connectionConfig, new MQModelHandleLibaryCore.Model.QueueModel()
            {
                QueueName = "exd_mscourtrecord,dan_sub",
                Qdurable = true,
                QautoDelete = false,
                Qexclusive = false,
                ArgumentList = arguments
            });
            string message = rPCRSend.CallAsync(SourceName.ToLower() + "@@@" + Content + "@@@" + rndStr, "Paragraphanalysis").Result;
            return message;
        }
    }
}
